package com.sf.controller;

import com.sf.domain.Product;
import com.sf.qo.QueryObject;
import com.sf.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productServiceImpl;

    @RequestMapping("/list")
    public String list(Model model, QueryObject queryObject) {
        model.addAttribute("pageResult", productServiceImpl.selectByPage(queryObject));
        return "product/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id){
        // 修改
        if (id != null) {
            Product product = productServiceImpl.selectById(id);
            model.addAttribute("product", product);
        }
        return "/WEB-INF/views/product/input.jsp";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Product product){
        if (product.getId() == null) {
            productServiceImpl.save(product);
        } else {
            productServiceImpl.update(product);
        }
        return "redirect:/WEB-INF/views/product/list.jsp";
    }
    
    @RequestMapping("/details")
    public String details(Model model, Long id){
        Product product = productServiceImpl.selectById(id);
        model.addAttribute("product", product);
        return "/WEB-INF/views/product/details.jsp";
    }


}
